package com.cashmate.struts;

import com.cashmate.hibernate.dao.CheckDAO;
import com.cashmate.hibernate.dao.DocumentDAO;
import com.cashmate.hibernate.dao.LoansDAO;
import com.cashmate.hibernate.pojo.Checks;
import com.cashmate.hibernate.pojo.Documents;
import com.cashmate.hibernate.pojo.Loans;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;


import com.opensymphony.xwork2.ActionSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.struts2.ServletActionContext;

public class Register_EditCheck extends ActionSupport{
  private LoansDAO loanDAO = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
  private CheckDAO checkDAO = (CheckDAO) ServletActionContext.getServletContext().getAttribute("checksDAO");
  private DocumentDAO docDAO = (DocumentDAO) ServletActionContext.getServletContext().getAttribute("documentDAO");
  private String oper;
  private String checkno;
  private String bankaccountno;
  private String checkdate;
  private int id;
  private String loc;
  private SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

  @Actions( {
    @Action(value = "/editcheck", results = {
      @Result(location = "${loc}", name = "success"), @Result(location = "${loc}", name = "input")
    })
  })

  @Override
  public String execute() throws Exception{
      loc = "../../loancheckregister.jsp?cat=check";
      System.out.println("UNIF: " + bankaccountno + checkno);
      Loans loan = loanDAO.get(id);
      if(loan.getCheckses().size() < 1){
          if(bankaccountno.length() > 0 && checkno.length() > 0){
              Documents checkD = new Documents();
              checkD.setDocumentdate(format.format(new Date()));
              checkD.setDocumentno(checkno);
              checkD.setDocumenttype("Check");
              checkD.setIsactive(true);
              docDAO.insert(checkD);
              
              Checks check = new Checks();
              check.setCheckno(checkno); 
              check.setCheckdate(format.format(new Date()));
              check.setBankaccountno(bankaccountno);
              check.setCheckamount(loan.getNetloanableamount());
              check.setLoans(loan);              
              check.setDocuments(checkD);
              checkDAO.insert(check);              
              System.out.println("IF: " + bankaccountno + checkno);
              loan.getCheckses().add(check);
              loanDAO.update(loan);
          }          
          System.out.println("IF1: " + bankaccountno + checkno);
      }else{
          if(bankaccountno.length() > 0 && checkno.length() > 0){
              for(Checks c : loan.getCheckses()){
                  System.out.println("Else:" + bankaccountno + checkno );
                  c.setBankaccountno(bankaccountno);
                  c.setCheckno(checkno);
                  checkDAO.update(c);
              }
          }
          System.out.println("Else1:" + bankaccountno + checkno );
      }
      
      
      
      return SUCCESS;
  }

    public void setOper(String oper) {
        this.oper = oper;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLoc() {
        return loc;
    }

    public void setBankaccountno(String bankaccountno) {
        this.bankaccountno = bankaccountno;
    }

    public void setCheckno(String checkno) {
        this.checkno = checkno;
    }

    public void setCheckdate(String checkdate) {
        this.checkdate = checkdate;
    }


}
